import qs from 'qs'
import axios from 'axios'

let baseUrl = 'http://localhost:8006';

//ajax 调用
const service = axios.create({
    baseURL:baseUrl,
    timeout:30000
});

service.apiUrl = baseUrl;
let keys = []

//请求参数处理
service.interceptors.request.use((config)=>{
    let data={};
    let keys = [];
    if (config.method === 'get') {
        // url参数签名
        data = config.data = Object.assign(config.data ? config.data : {}, data)
        keys = Object.keys(data)
    } else {
        // request body参数的内容
        data = config.data = Object.assign(config.data ? config.data : {}, data)
        keys = Object.keys(data)
    }

    // 排序
    const skeys = keys.sort()
    let str = ''
    skeys.filter(k => {
        return true;
    }).map(k => {
        const v = data[k]
        if (v || v === 0) {
            // 参数值为空，则不参与签名
            str = str + k + '=' + v + '&'
        }
    });
    config.headers['Content-Type'] ='application/x-www-form-urlencoded; charset=UTF-8'; //application/json
   // config.headers['Content-Type'] = 'application/json'; //
    //config.params = qs.stringify(str+"a=1");
     
    //console.log("str:" + config.data);
    // config.params = JSON.stringify(config.data) ;
 
    console.log("str:" + qs.stringify(config.data));
    config.params = qs.stringify(config.data, { indices: false });
    return config
});

//返回参数处理
service.interceptors.response.use((response)=>{
    console.log(response.data)
    return Promise.resolve(response.data);
});

export default service




 